package com.haoxi.gateway.test.gatewayserver;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.TimerTask;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.haoxi.gateway.common.TestConfConst;
import com.haoxi.gateway.test.message.CMPP30DeliverMessage;

public class SendDeliverTask extends TimerTask {
    private Socket client;
    private OutputStream out;

    protected static final Log log = LogFactory.getLog(SendDeliverTask.class);

    public SendDeliverTask(Socket client,OutputStream out) {
        this.client = client;
        this.out=out;
    }

    public void run() {
        try {
            if (null == client || client.isClosed()) {

                return;
            }
            if (client.isBound() && client.isConnected()) {
                CMPP30DeliverMessage deliver = new CMPP30DeliverMessage(
                        TestConfConst.dest_Id, TestConfConst.service_Id, 1, 1,
                        TestConfConst.msgFmt, TestConfConst.src_terminal_Id,
                        TestConfConst.src_terminal_type, 0,
                        TestConfConst.msg_Content.getBytes(),
                        TestConfConst.linkID);

                //OutputStream out = client.getOutputStream();
                for (int i = 0; i < TestConfConst.delivertimes; i++) {
                    deliver = new CMPP30DeliverMessage(TestConfConst.dest_Id,
                            TestConfConst.service_Id, 1, 1,
                            TestConfConst.msgFmt,
                            TestConfConst.src_terminal_Id,
                            TestConfConst.src_terminal_type, 0,
                            TestConfConst.msg_Content.getBytes(),
                            TestConfConst.linkID);
                    out.write(deliver.getBytes());
                    out.flush();
                    log.debug(deliver.toString());
                    Thread.sleep(20);
                }
            }

        } catch (IOException ioe) {
            log.error("ioexception", ioe);
        } catch (InterruptedException ex) {
            log.error("InterruptedException", ex);
        }
    }

}
